const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()

exports.main = async (event, context) => {
  const { username, password } = event

  try {
    // 从数据库中查询用户
    const userRes = await db.collection('users').where({
      username: username,
      password: password  // 实际应用中应该使用加密后的密码
    }).get()

    if (userRes.data.length > 0) {
      return {
        success: true,
        userInfo: {
          username: userRes.data[0].username,
          role: userRes.data[0].role
        }
      }
    } else {
      return {
        success: false,
        message: '用户名或密码错误'
      }
    }
  } catch (err) {
    console.error(err)
    return {
      success: false,
      message: '登录失败'
    }
  }
} 